package com.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.bstek.bdf2.core.context.ContextHolder;
import com.bstek.bdf2.core.orm.jdbc.JdbcDao;
import com.entity.RuleFile;

@Component
@Transactional
public class RfileDao extends JdbcDao {

	public List<Map<String, Object>> loadAlluser() {
		String sql = "select * from bdf2_user";
		return this.getJdbcTemplate().queryForList(sql);
	}

	public List<RuleFile> loadRuleFile() {
		String sql = "select * from RULE_FILE";
		return this.getJdbcTemplate().query(sql, new RowMapper<RuleFile>() {
			public RuleFile mapRow(ResultSet rs, int rowNum) throws SQLException {
				// TODO Auto-generated method stub
				RuleFile rf = new RuleFile();
				rf.setId(rs.getString("ID_"));
				rf.setCreateDate(rs.getDate("CREATE_DATE_"));
				rf.setCreateUser(rs.getString("CREATE_USER_"));
				rf.setUpdateDate(rs.getDate("UPDATE_DATE_"));
				rf.setUpdateUser(rs.getString("UPDATE_USER_"));
				rf.setDescription(rs.getString("DESCRIPTION_"));
				rf.setFileName(rs.getString("FILE_NAME_"));
				rf.setFileType(rs.getInt("FILE_TYPE_"));
				rf.setFilePath(rs.getString("FILE_PATH_"));
				return rf;
			}
		});
	}

	public void saveNewFile(String filename, int type, String filePath) {
		String sql = "insert into RULE_FILE(ID_,CREATE_DATE_,CREATE_USER_,UPDATE_DATE_,UPDATE_USER_,DESCRIPTION_,FILE_NAME_,FILE_TYPE_,FILE_PATH_) values(?,?,?,?,?,?,?,?,?)";
		this.getJdbcTemplate().update(sql, new Object[] { UUID.randomUUID().toString(), new Date(),
				ContextHolder.getLoginUserName(), null, null, null, filename, type, filePath });
	}

	public void deleteRuleFile(String fileId) {
		String sql = "delete from RULE_FILE where ID_=?";
		this.getJdbcTemplate().update(sql, new Object[] { fileId });
	}
}
